package cn.yunyichina.provider.hquery.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;

import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.hquery.vo.ClinicQueueVoResp;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.ClinicQueueVo;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.ClinicQueueVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;
/**
 * Created by chenjx on 2017/4/11
 * 门诊信息查询功能
 */
@Service("clinicServiceImpl")
public class ClinicServiceImpl {

	private static final Logger logger = LoggerFactory.getLogger(ClinicServiceImpl.class);
	
	/*
	    门诊候诊信息查询接口
	*/
	public ResponseObject getClinicQueue(String data) {
		logger.info("门诊候诊信息查询 getClinicQueue data: " + data);
		
        ResponseObject response = new ResponseObject();
    	
        ExaminationVo requestVo = JsonUtils.parseObject(data, ExaminationVo.class);
        if (StringUtils.isBlank(requestVo.getHospitalCode())
                ||StringUtils.isBlank(requestVo.getPatCardNo())
                ||StringUtils.isBlank(requestVo.getPatCardType())){
            response.setResultCodeAndMessage("18002","hospitalCode,patCardNo,patCardType不能为空");
            return response;
        }
        
        //结果集 门诊候诊信息
        List<ClinicQueueVoResponse> clinicQueueVoResponseList = null;
        
        //检验列表查询
        try {
        	ClinicQueueVo clinicQueueListVo = new ClinicQueueVo();
        	clinicQueueListVo.setHospitalCode(requestVo.getHospitalCode());
        	clinicQueueListVo.setBranchCode(requestVo.getBranchCode());
        	clinicQueueListVo.setPatCardType(requestVo.getPatCardType());
        	clinicQueueListVo.setPatCardNo(requestVo.getPatCardNo());
            
            logger.info("调用调度中心开始" + JsonUtils.toJsonString(clinicQueueListVo));
            ResCommon<List<ClinicQueueVoResponse>> clinicQueueList  = HisifaceService.getHisifaceClient().getClinicQueue(clinicQueueListVo);
            logger.info("调用调度中心结束");
            if (!"0".equals(clinicQueueList.getResultCode())){
                logger.info("调用调度中心产生异常"+ clinicQueueList.getResultMessage());
                response.setResultCodeAndMessage("18998", clinicQueueList.getResultMessage());
                return response;
            }
            logger.info("调用调度中心返回 "+JsonUtils.toJsonString(clinicQueueList));
            
            clinicQueueVoResponseList = clinicQueueList.getResult();
            
        } catch (Exception ex) {
            response.setResultCodeAndMessage("18999","系统异常");
            return  response;
        }
        
        //接口返回字段名调整
        List<ClinicQueueVoResp> clinicQueueVoRespList = new ArrayList<ClinicQueueVoResp>();
        if(CollectionUtils.isNotEmpty(clinicQueueVoResponseList)){
        	
        	ClinicQueueVoResp clinicQueueVoResp = null;
            for(ClinicQueueVoResponse clinicQueueVoResponse:clinicQueueVoResponseList){
            	clinicQueueVoResp = new ClinicQueueVoResp();
            	clinicQueueVoResp.setHisOrderNo(clinicQueueVoResponse.getHisOrdNum());
            	clinicQueueVoResp.setDeptName(clinicQueueVoResponse.getDeptName());
            	clinicQueueVoResp.setDeptLocation(clinicQueueVoResponse.getDeptLocation());
            	clinicQueueVoResp.setDoctorName(clinicQueueVoResponse.getDoctorName());
            	clinicQueueVoResp.setSerialNum(clinicQueueVoResponse.getSerialNum());
            	clinicQueueVoResp.setCurrentNum(clinicQueueVoResponse.getCurrentNum());
            	clinicQueueVoResp.setVisitTime(clinicQueueVoResponse.getVisitTime());
            	clinicQueueVoResp.setFrontNum(clinicQueueVoResponse.getFrontNum());
            	
            	clinicQueueVoRespList.add(clinicQueueVoResp);
            }
        }
        response.setResultCodeAndMessageAndResult("0","成功",clinicQueueVoRespList);
        logger.info("【门诊候诊信息查询】结束, 返回结果=" + JsonUtils.toJsonString(response));
        return response;
	}
}
